package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author atguigu-mqx
 * @ClassName AccountReceiver
 * @description: TODO
 * @date 2024年06月04日
 * @version: 1.0
 */
@Component
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 监听消息实现用户账户充值
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void rechargePaySuccess(ConsumerRecord<String,String> record){
        //  获取发送消息.
        String orderNo = record.value();
        if (!StringUtils.isEmpty(orderNo)){
            //  调用服务层方法.
            userAccountService.rechargePaySuccess(orderNo);
        }
    }

    /**
     * 监听消息实现用户账户初始化
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void userRegister(ConsumerRecord<String,String> record){
        //  获取发送消息.
        String userId = record.value();
        if (!StringUtils.isEmpty(userId)){
            //  调用服务层方法.
            userAccountService.initUserAccount(userId);
        }
    }

    /**
     * 监听消息实现用户账户扣减
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void accountMinus(ConsumerRecord<String,String> record){
        //  获取发送的数据
        String orderNo = record.value();
        //  判断
        if (!StringUtils.isEmpty(orderNo)){
            //  调用服务层方法
            userAccountService.minus(orderNo);
        }
    }

    /**
     * 监听消息实现用户账户解锁
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void accountUnlock(ConsumerRecord<String,String> record){
        //  获取发送的数据
        String orderNo = record.value();
        //  判断
        if (!StringUtils.isEmpty(orderNo)){
            //  调用服务层方法
            userAccountService.accountUnlock(orderNo);
        }
    }
}
